#include <stdlib.h>

#include "lib.h"
#include "bratsql.h"
#include "bratgame.h"
#include "odbc.h"
#include "missinggamenums.h"

BratSQL::BratSQL(const String & dsn, const String & dblogin,
	const String & dbpasswd)
	: TrackSQL(dsn, dblogin, dbpasswd)
{
#ifdef USE_MISSINGGAMENUMS
	//missing game nums deleted in TrackSQL::~TrackSQL
	missinggamenums = new MissingGameNums(this, "pub_getTableIdentityBaccarat");
#endif
}

int32
BratSQL::game(int32 casinoId, PID pid, bool8 forMoney, SESSIONID sessionId,
	int32 compFactor,
	int32 numBankerCards, uint8* banker,
	int32 numPlayerCards, uint8* player,
	int32* bets, int32* payouts, int32 numBetsPayouts,
	int32 commission, int32 outcome,
	int32 balanceAdjust, int32* balance, int32* comp)
{
	bool8 ok = false;
	ODBCConnection* conn;
	ODBCQuery* query;
	#define GAMEID_SIZE		16
	typedef char GAME_ID[GAMEID_SIZE];
	GAME_ID gid = { 0 };

	conn = getConn();
	if (!conn)
		return NoConnection;

	query = conn->query();
	if (query)
	{
		query->define("exec pub_CreateGameBaccarat ");
		query->addParam(casinoId);
		query->addParam(pid);
		query->addParam((bool8)(forMoney != 0));
		query->addParam(sessionId, ODBCQuery::NOQUOTES);
		query->addParam(compFactor);
		query->addParam(banker, numBankerCards, ODBCQuery::BINARY);
		query->addParam(player, numPlayerCards, ODBCQuery::BINARY);
		query->addParam(bets[BratGame::BANKER], ODBCQuery::MONEY);
		query->addParam(bets[BratGame::PLAYER], ODBCQuery::MONEY);
		query->addParam(bets[BratGame::TIE], ODBCQuery::MONEY);
		query->addParam(payouts[BratGame::BANKER], ODBCQuery::MONEY);
		query->addParam(payouts[BratGame::PLAYER], ODBCQuery::MONEY);
		query->addParam(payouts[BratGame::TIE], ODBCQuery::MONEY);
		query->addParam(commission, ODBCQuery::MONEY);
		query->addParam((uint8)(outcome & 0xff), ODBCQuery::BINARY);
		query->addParam(balanceAdjust, ODBCQuery::MONEY);

		if (query->exec())
		{
			while (query->getRow())
			{
				if (query->getData(1, gid, GAMEID_SIZE) &&
					query->getData(2, balance, ODBCQuery::MONEY) &&
					query->getData(3, comp))
					ok = true;
			}
		}
		delete query;
	}
	delete conn;

#ifdef USE_MISSINGGAMENUMS
	if (ok)
	{
		int32 gamenum = atol(gid);
		if (missinggamenums)
			missinggamenums->setGameNum(gamenum);
	}
#endif

	return ok ? NoError : ExecFailed;
}


int32
BratSQL::trackGetOptions(int32* tiePayout)
{
    int32 result = ExecFailed;

    ODBCConnection* conn;
    ODBCQuery* query;
    bool8 ok = false;

    int32 int1;

	conn = getConn();
	if (!conn)
		return NoConnection;

    query = conn->query();
    if (query)
    {
        query->define("exec pub_BaccaratGetOptions");

        if (query->exec())
        {
            while (query->getRow())
            {
                ok = query->getData(1, &int1);

                if (ok)
                {
                    *tiePayout = (int32)int1;
                }
            }
        }

        delete query;
    }

    delete conn;

    if(ok)
	{
		result = NoError;
	}
	else
	{
		result = ExecFailed;
	}

	return result;
}
